
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uast.bpdex.actions.addUE1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.util.regex.*;
import uast.bpdex.forms.addUE1;
import uast.bpdex.BaseDatos.*;
import uast.bpdex.Profesor;
import uast.bpdex.Obrero;
import uast.bpdex.Empleado;
import java.io.*;

/**
 *
 * @author carlos
 */
public class addUE1Action extends org.apache.struts.action.Action {
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String target = "failAddU";
        String[] valores;
        int i;
        Pattern p;
        Matcher m;
        DataBase db;
        Profesor prof;
        Obrero obr;
        Empleado empl;

        if(form == null){
          return(mapping.findForward(target));
        }
        addUE1 formA = (addUE1)form;
        valores = new String[13];
        valores[0] = formA.getUsr();
        valores[1] = formA.getPssw();
        valores[2] = formA.getNombre();
        valores[3] = formA.getApellido();
        valores[4] = formA.getCi();
        valores[5] = formA.getSexo();
        valores[6] = formA.getEmail();
        valores[7] = formA.getTlf();
        valores[8] = formA.getDpto();
        valores[9] = formA.getCord();
        valores[10] = formA.getTipo();
        valores[11] = formA.getPregunta();
        valores[12] = formA.getRespuesta();
               

        for(i = 0; i < valores.length - 1; ++ i){
          if(valores[i].equals("")){
            return(mapping.findForward(target));
          }
        }
        //Validacion de parametros recibidos
        //Se valida la cedula
        p = Pattern.compile("[v|V|e|E]-[0-9]+");
        m = p.matcher(valores[4]);
        if(!m.matches()){
          return(mapping.findForward(target));
        }
        //Se valida el email
        p = Pattern.compile(".+@.+\\..+");
        m = p.matcher(valores[6]);
        if(!m.matches()){
          return(mapping.findForward(target));
        }
        //Se valida el sexo
        p = Pattern.compile("M|F");
        m = p.matcher(valores[5]);
        if(!m.matches())
          return(mapping.findForward(target));
        
        db = new DataBase();
        
        //Verificar el tipo del elemento agregarlo a la base de datos
        try{
          if(valores[10].equals("prof")){
            prof = new Profesor(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8],valores[9], valores[11], valores[12]);
            db.agregarProfesor(prof); 
            target = "succAddU";
          }else if(valores[10].equals("obr")){
            obr = new Obrero(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8],valores[9], valores[11], valores[12]);
            db.agregarObrero(obr); 
            target = "succAddU";
          }else if(valores[10].equals("empl")){
            empl = new Empleado(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8],valores[9], valores[11], valores[12]);
            db.agregarEmpleado(empl); 
            target = "succAddU";           
          }
        }catch(Exception ex0){
          return (mapping.findForward(target));
        }
        return(mapping.findForward(target));
   }
}
